<template>
    <el-form ref="ruleFormRef" style="max-width: 600px" :model="ruleForm" :rules="rules" label-width="auto"
        class="demo-ruleForm" :size="formSize" status-icon>
        <el-form-item label="商品名称" prop="name">
            <el-input v-model="ruleForm.name" />
        </el-form-item>

        <el-form-item label="品牌名称" prop="region">
            <el-select v-model="ruleForm.region" placeholder="选择品牌名称">
                <el-option label="请选择" value="" />
                <el-option v-for="aa in Brandbind" :label="aa.BrandName" :value="aa.BrandId" />
            </el-select>
        </el-form-item>
        <el-form-item label="商品分类" prop="count">
            <div class="m-4">
                <p>Child options expand when hovered</p>
                <el-cascader v-model="value" :options="Typeoption" :props="props" @change="handleChange" />
            </div>
        </el-form-item>

        <el-form-item label="商品价格" prop="price">
            <el-input v-model="ruleForm.price" />
        </el-form-item>

        <el-form-item label="商品图片" prop="price">
        </el-form-item>

        <el-form-item label="状态" prop="resource">
            <el-radio-group v-model="ruleForm.resource">
                <el-radio value="启用">启用</el-radio>
                <el-radio value="禁用">禁用</el-radio>
            </el-radio-group>
        </el-form-item>

        <el-form-item>
            <el-button type="primary" @click="submitForm(ruleFormRef)">
                提交
            </el-button>
            <el-button @click="resetForm(ruleFormRef)">重置</el-button>
        </el-form-item>
    </el-form>
</template>

<script lang="ts" setup>
import { reactive, ref, onMounted } from 'vue'
import axios from 'axios'
import type { ComponentSize, FormInstance, FormRules } from 'element-plus'

onMounted(() => {
    BindBrand();
    handleChange();
})

const formSize = ref<ComponentSize>('default')
const ruleFormRef = ref<FormInstance>()
//响应式数据
const ruleForm = reactive({
    name: '',
    region: '',
    count: '',
    price: '',
    date1: '',
    date2: '',
    delivery: false,
    location: '',
    type: [],
    resource: '',
    desc: '',
})

const Brandbind = ref([])

const BindBrand = () => {
    axios({
        url: 'http://localhost:7052/api/Goods/GoodsTypeBind/GoodsTypeBind',
        method: 'get'
    })
        .then((res) => {
            Brandbind.value = res.data.Data;
            console.log(Brandbind);
        })
}
const value = ref([])

const props = {
  expandTrigger: 'hover' as const,
}

const handleChange = () => {
    axios({
        url: 'http://localhost:7052/api/Goods/ShowType/ShowType?pid=0',
        method: 'get'
    })
        .then((res) => {
            Typeoption.value = res.data.Data;
        })
}

const Typeoption = ref([])

//表单验证
const rules = reactive<FormRules>({
    name: [
        { required: true, message: '商品名称不能为空', trigger: 'blur' },
        { min: 3, max: 10, message: '商品名称名称长度需要3-10个字符', trigger: 'blur' },
    ],
    region: [
        {
            required: true,
            message: 'Please select Activity zone',
            trigger: 'change',
        },
    ],
    count: [
        {
            required: true,
            message: 'Please select Activity count',
            trigger: 'change',
        },
    ],
    date1: [
        {
            type: 'date',
            required: true,
            message: 'Please pick a date',
            trigger: 'change',
        },
    ],
    date2: [
        {
            type: 'date',
            required: true,
            message: 'Please pick a time',
            trigger: 'change',
        },
    ],
    location: [
        {
            required: true,
            message: 'Please select a location',
            trigger: 'change',
        },
    ],
    type: [
        {
            type: 'array',
            required: true,
            message: 'Please select at least one activity type',
            trigger: 'change',
        },
    ],
    resource: [
        {
            required: true,
            message: 'Please select activity resource',
            trigger: 'change',
        },
    ],
    desc: [
        { required: true, message: 'Please input activity form', trigger: 'blur' },
    ],
})

//提交事件
const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            console.log('submit!')
        } else {
            console.log('error submit!', fields)
        }
    })
}
//重置事件
const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
}

const options = Array.from({ length: 10000 }).map((_, idx) => ({
    value: `${idx + 1}`,
    label: `${idx + 1}`,
}))
</script>